package kr.co.doriclassic.chapter18;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;

public class PanelJTable extends JPanel implements ActionListener {
	private JTable table;
	private JButton btnAdd;

	public PanelJTable() {
		initialize();
		tableCellAlignAndWidth();
	}
	
	private void tableCellAlignAndWidth() {//간격의 설정
		//가운데 정렬
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		//우측정렬
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		TableColumnModel cModel = table.getColumnModel();
		
		int columCount = cModel.getColumnCount();//컬럼의 갯수
		
		//추후 반복문 필요
		cModel.getColumn(0).setPreferredWidth(70);//사원번호 간격
		cModel.getColumn(0).setCellRenderer(dtcrCenter);//정렬적용		
		
		cModel.getColumn(1).setPreferredWidth(70);//사원명 간격
		cModel.getColumn(1).setCellRenderer(dtcrCenter);//정렬적용		
		
		cModel.getColumn(2).setPreferredWidth(70);//직급 간격
		cModel.getColumn(2).setCellRenderer(dtcrCenter);//정렬적용		
		
		cModel.getColumn(3).setPreferredWidth(70);//매니저 간격
		cModel.getColumn(3).setCellRenderer(dtcrCenter);//정렬적용		
		
		cModel.getColumn(4).setPreferredWidth(150);//급여 간격
		cModel.getColumn(4).setCellRenderer(dtcrRight);//정렬적용		
	}



	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_Top = new JPanel();
		add(panel_Top, BorderLayout.NORTH);
		
		btnAdd = new JButton("추가");
		btnAdd.addActionListener(this);
		panel_Top.add(btnAdd);
		
		JPanel panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		final JLabel lblSelected = new JLabel("New label");
		panel_Bottom.add(lblSelected);
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		
		String[] colNames = {"사원번호","사원명","직책","매니저","급여","부서번호"};
		TableModel model = new DefaultTableModel(colNames, 0){
			//컬럼인덱스의 타입을 가져온다
			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return getValueAt(0, columnIndex).getClass();//Integer.class
			}			
		};
		
		
		table = new JTable(model);
		table.setAutoCreateRowSorter(true);//탭정렬
		
		//마우스 이벤트 어댑터
		table.addMouseListener(new MouseAdapter() {//지정한 메소드만 실행
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				int row = table.getSelectedRow();//행
				/*
				int row = table.getSelectedRow();//행
				int col = table.getSelectedColumn();//열
				try {
					String text = (String) table.getModel().getValueAt(row, col);
					lblSelected.setText(text);
				} catch (ClassCastException e2) {
					int value =(int) table.getModel().getValueAt(row, col);
					lblSelected.setText(value+"");
				}*/
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < table.getColumnCount(); i++) {
					sb.append(table.getModel().getValueAt(row, i)+" ");
				}
				lblSelected.setText(sb.toString());
			}			
		});
		
		scrollPane.setViewportView(table);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			actionPerformedBtnNewButton(e);
		}
	}
	protected void actionPerformedBtnNewButton(ActionEvent e) {
		DecimalFormat df = new DecimalFormat("#,###");//금액 세자리마다 쉼표 찍기
		
		DefaultTableModel model = (DefaultTableModel)table.getModel();
		Object[] newRow = {
				new Integer((int)(Math.random()*100)+1),//사원번호
				"김태희",//이름
				"사원",//직책
				new Integer((int)(Math.random()*100)+1),
				df.format(new Integer((int)(Math.random()*100)+1)),
				new Integer((int)(Math.random()*100)+1)
			};
		model.addRow(newRow);
		}
	}
